// 导出页面为PDF格式
import html2Canvas from 'html2canvas';
import JsPDF from 'jspdf';
const htmlToPdf = {
	getPdf: (obj) => {
		return new Promise((resolve, reject) => {
			let title = `${obj.match_name}-${obj.project_name}`
			// get_imgObj().then((imgObj)=>{
			// console.log(imgObj)
			// htmlToPdf.getpdf_secondStage_imgObj(obj).then((secondStage_imgObj)=>{
			//console.log(headers_imgObj)
			console.log(obj)
			let scale = null
			if (obj.projectTypeId == 1) {
				let merchant = obj.merchant
				if (merchant <= 3) {
					scale = 2
				} else if (merchant >= 4 && merchant <= 6) {
					scale = 1.2
				} 
				// else if (merchant >= 7 && merchant <= 8) {
				// 	scale = 0.78
				// } 
				// 20210430修改，无论最后怎样都是0.78
				else {
					scale = 0.78
					// scale = 0.4
				}
			} else if (obj.projectTypeId == 5) {
				let merchant = obj.merchant
				if (merchant <= 3) {
					scale = 2
				} else if (merchant >= 4 && merchant <= 6) {
					scale = 1.1
				} else if (merchant >= 7 && merchant <= 8) {
					scale = 0.78
				} else {
					scale = 0.4
				}
			} else {
				if (obj.merchant <= 3) {
					scale = 2
				} else if (obj.merchant >= 4 && obj.merchant <= 6) {
					scale = 1.4
				} else {
					scale = 0.75
				}
			}
			// document.querySelector("#" + obj.id).children[0].remove();
			// document.querySelector("#" + obj.id).children[1].remove();
			// let domId = "";
			// if(!obj.id){
			// 	domId = "pdfDom"+obj.merchant
			// }else{
			// 	domId = obj.id
			// }
			// html2Canvas(document.querySelector("#" + domId + ""), {
			html2Canvas(document.querySelector("#" + obj.id + ""), {
				async: false,
				allowTaint: true,
				// width:1600,
				scale: scale,
			}).then(function(canvasObj) {
				let contentWidth = canvasObj.width
				//console.log(canvasObj.width)
				let contentHeight = canvasObj.height
				console.log(contentHeight)
				//一页pdf显示html页面生成的canvas高度;
				let pageHeight = contentWidth / 592.28 * 841.89
				let pageCount = 0
				//未生成pdf的html页面高度
				let leftHeight = contentHeight
				let leftWidth = 0
				//页面偏移
				// let position = 16
				let position = 0
				//a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
				let imgWidth = 592.28
				let imgHeight = 592.28 / contentWidth * contentHeight
				// console.log(imgHeight)
				let pageData = canvasObj.toDataURL('image/jpeg', 1.0)
				// console.log(pageData)

				let blob = dataURLtoBlob(pageData);
				const url = URL.createObjectURL(blob)


				//转img 2 pdf
				let PDF = new JsPDF('', 'pt', 'a4')
				//有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
				//当内容未超过pdf一页显示的范围，无需分页
				if (leftHeight < pageHeight) {
					// console.log(pageData)
					PDF.addImage(url, 'JPEG', 0, 0, imgWidth, imgHeight)
				} else {
					while (leftHeight > 0) {
						if (pageCount > 0) { //大于第一页
							if (obj.projectTypeId == 5) {
								if (pageCount == 3) {
									position = position + 10;
								}
							}
							if (pageCount == 4) {
								position = position + 20;
							}
							// PDF.addImage(pageData, 'JPEG', leftWidth, position, imgWidth, imgHeight)
							PDF.addImage(url, 'JPEG', leftWidth, position, imgWidth, imgHeight)
							leftHeight -= pageHeight
							position -= 841.89
							// position -= 825.89
							pageCount++
							//避免添加空白页
							if (leftHeight > 0) {
								PDF.addPage()
							}
						} else {
							// imgHeight = 592.28 / contentWidth * contentHeight
							// PDF.addImage(pageData, 'JPEG', leftWidth, position, imgWidth, imgHeight)
							PDF.addImage(url, 'JPEG', leftWidth, position, imgWidth, imgHeight)
							leftHeight -= pageHeight
							position -= 841.89
							// position -= 825.89
							pageCount++
							//避免添加空白页
							if (leftHeight > 0) {
								PDF.addPage()
							}
						}
					}
				}
				PDF.save(title + '.pdf')
				resolve()
			})
			// })
			// })
		})
	},
}
function dataURLtoBlob(dataurl) {
	var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
	bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
	while(n--){
	  u8arr[n] = bstr.charCodeAt(n);
	}
  return new Blob([u8arr], {type:mime});
} 
export default htmlToPdf
